package com.sdy.dataexchange.meta.service.impl;

import com.sdy.dataadapter.DataAdapter;
import com.sdy.dataexchange.biz.model.ExDbDict;
import com.sdy.dataexchange.meta.mapper.MetadataColumnMapper;
import com.sdy.dataexchange.meta.model.MetadataColumn;
import com.sdy.dataexchange.meta.model.OracleTabColumns;
import com.sdy.dataexchange.meta.service.MetadataColumnService;
import com.sdy.dataexchange.meta.util.ConstantUtil;
import com.sdy.dataexchange.meta.util.JdbcUtil;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: 王越洋
 * @version: v1.0
 * @description: com.sdy.dataexchange.meta.service.impl
 * @date:2019/10/23
 */
@Service
public class MetadataColumnServiceImpl extends BaseServiceImpl<MetadataColumn> implements MetadataColumnService  {
    @Autowired
    private JdbcUtil jdbcUtil;
    @Autowired
    private MetadataColumnMapper metadataColumnMapper;

    @Override
    public List<MetadataColumn> queryMysqlTabColumns(ExDbDict exDbDict, String table) throws InstantiationException, IllegalAccessException {
        String dbName = "'"+exDbDict.getDbName()+"'";
        String tableName = "'"+table+ "'";
        DataAdapter dataAdapter = jdbcUtil.getDataAdapter(exDbDict);
        List<MetadataColumn> mysqlTabColumns = dataAdapter.executeQuery(String.format(ConstantUtil.MYSQL_TABLE_COLUMNS, dbName,tableName), MetadataColumn.class);
        return mysqlTabColumns;
    }

    @Override
    public List<MetadataColumn> queryOracleTabColumns(ExDbDict exDbDict, Integer id, String table) throws InstantiationException, IllegalAccessException {
        DataAdapter dataAdapter = jdbcUtil.getDataAdapter(exDbDict);
        String columns = "'" + table + "'";
        List<MetadataColumn> oracleTabColumns = dataAdapter.executeQuery(String.format(ConstantUtil.ORACLR_TABLE_COLUMNS, columns, columns), MetadataColumn.class);
        return oracleTabColumns;
    }

    @Override
    public Integer addOracleTabColumns(List<MetadataColumn> metadataColumns) {
        return null;
    }


    @Override
    public Integer addOneTabColumns(MetadataColumn metadataColumn, Integer modId) {
        return metadataColumnMapper.addOneTabColumns(metadataColumn, modId);
    }

    @Override
    public List<OracleTabColumns> queryOracleTab(ExDbDict exDbDict) {
        return null;
    }

    @Override
    public List<OracleTabColumns> queryMysqlTab(ExDbDict exDbDict) {
        return null;
    }

    /**
     * 删除已删除的 表字段
     * @param colId
     * @return
     */
    @Override
    public Integer delByColIdColumn(Integer colId) {
        return metadataColumnMapper.delByColIdColumn(colId);
    }

    @Override
    public Integer delAllTabColums(Integer modId) {
        return metadataColumnMapper.delAllTabColums(modId);
    }

    @Override
    public List<MetadataColumn> queryByModId(Integer modId) {
        return metadataColumnMapper.queryByModId(modId);
    }

    @Override
    public Integer updByIdTabColumns(Integer columnId, Integer type, MetadataColumn column) {
        return metadataColumnMapper.updByIdTabColumns(columnId, type, column);
    }
}
